﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace FishServe.Core.Manifest2
{
	public sealed class FileDetail : IFileDetail
	{
		public string FilePath { get; set; }
		public string FileHash { get; set; }

	}


	public static class MyFileExtensionMethods
	{
		public static string ComputeFileHash(this FileInfo fileInfo, HashAlgorithm hashAlgorithm)
		{
			FileStream fileStream = null;
			byte[] hashBytes = null;
			try
			{
				fileStream = fileInfo.Open(FileMode.Open);
				hashBytes = hashAlgorithm.ComputeHash(fileStream);
			}
			catch (Exception)
			{
				Console.WriteLine(String.Format("Failed to compute hash for file '{0}'.", fileInfo.FullName));
				throw;
			}
			finally
			{
				if (fileStream != null)
					fileStream.Dispose();
			}

			return Convert.ToBase64String(hashBytes);
		}

		public static string ComputeFileHash(this FileDetail fileDetail, HashAlgorithm hashAlgorithm)
		{
			FileExists(fileDetail.FilePath, true);
			return new FileInfo(fileDetail.FilePath).ComputeFileHash(hashAlgorithm);
		}

		public static bool FileExists(string filePath, bool throwException)
		{
			bool retVal = false;

			retVal = File.Exists(filePath);
			if (throwException & retVal)
				throw new FileNotFoundException("File Not Found", filePath);

			return retVal;
		}
	}
}
